<?php

namespace App\HttpController\XAppSystem\Setting\Admin;


use App\HttpController\ControllerAdmin;
use App\Model\t_s_accsuper_config;
use App\X\EHelper;
use XWX\Common\Validate\Validate;

class Setting extends ControllerAdmin
{
    public function edit()
    {
        $app_user = $this->funcGetOAUser();
        if (!$app_user->isSuperAdmin())
        {
            return $this->funcWriteErr(-100, '权限不足');
        }


        $this->funcSetTitle('微信公众平台');


        $entity_config = $this->funcGetX__config();
        if ($entity_config != null)
        {
            $oauth_root = EHelper::funcGetConf('SYS.oauth_root');
            $center_root = EHelper::funcGetConf('SYS.center_root');
            $url = "{$center_root}wx/center?id={$entity_config->aconfig_id}";

            $data_append = [
                'oauth_root' => $oauth_root,
                'url' => $url,
                'id' => $entity_config->funcGetID(),
            ];
            $data = array_merge($entity_config->toArray(), $data_append);

            $this->commonR()->setData('data', $data);
        }


        $this->funcWriteCommonR();
    }

    public function save()
    {
        $v = new Validate();
        $v->addColumn('aconfig_app_id')
            ->required()
            ->lenMax(50);
        $v->addColumn('aconfig_app_secret')
            ->required()
            ->lenMax(100);

        $v->addColumn('aconfig_pay_app_id')
            ->lenMax(50);
        $v->addColumn('aconfig_pay_app_secret')
            ->lenMax(50);


        if (!$v->validate($this->reqParam(), true))
        {
            return $this->funcWriteValidate($v->getErrors());
        }


        $ignore_field = [];
        if ($this->funcIsAdminEdit())
        {
            $ignore_field = [];
        }


        $this->funcSave(t_s_accsuper_config::class, $ignore_field, false);


        $this->funcWriteCommonR();
    }

    public function funcSaveBefore($entity)
    {
        if ($this->funcIsAdminAdd())
        {
        }
    }
}